package com.vvlive.caibao.smartcity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.vvlive.caibao.smartcity.bean.Person;
import com.vvlive.caibao.smartcity.utils.ImageUtil;
import com.vvlive.caibao.smartcity.utils.ViewHolderUtil;

import java.util.ArrayList;
import java.util.List;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/7/3.
 */
public class GroupMemberSelectAdapter extends BaseAdapter {

    private CheckedChangeListener checkedChangeListener;
    private List<Integer> indexes;
    private Context context;
    private List<Person> persons;

    public GroupMemberSelectAdapter(Context context, List<Person> persons) {
        checkedChangeListener = new CheckedChangeListener();
        indexes = new ArrayList<Integer>();
        this.context = context;
        this.persons = persons;
    }

    @Override
    public int getCount() {
        return persons.size();
    }

    @Override
    public Object getItem(int position) {
        return persons.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_groups_list, null, false);
        }
        Person person = (Person) getItem(position);

        ImageView avatar = ViewHolderUtil.get(convertView, R.id.group_member_avatar);
        avatar.setTag(person.getPortrait());
        TextView name = ViewHolderUtil.get(convertView, R.id.group_member_name);
        TextView mood = ViewHolderUtil.get(convertView, R.id.group_member_mood);
        CheckBox select = ViewHolderUtil.get(convertView, R.id.group_member_select);
        select.setTag(position);
        select.setOnCheckedChangeListener(checkedChangeListener);

        ImageUtil.loadImage(context, avatar, 0);
        name.setText(person.getName());
        mood.setText("How duang today!!!");

        return convertView;
    }

    private final class CheckedChangeListener implements OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int index = (int) buttonView.getTag();
            if(isChecked) {
                indexes.add(index);
            }else{

            }
        }
    }

    public List<String> getSelectedItems(){
        List<String> members = new ArrayList<String>();
        for (Integer i : indexes){
            members.add(((Person) getItem(i)).getNumber());
        }
        return members;
    }
}
